home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / tvcolor.exe / TDEMO.CPP < prev    next >
C/C++ Source or Header  |  1992-06-27  |  21KB  |  733 lines

  1. /*-----------------------------------------------------------------------*/
  2. /* filename -       TDemo.cpp                                            */
  3. /*                                                                       */
  4. /* function(s)                                                           */
  5. /*                  definitions for TDemo class                          */
  6. /*                  demonstrates use of the TVColorDialog library        */
  7. /*-----------------------------------------------------------------------*/
  8.  
  9. /*-----------------------------------------------------------------------*/
  10. /*                                                                       */
  11. /*    TDEMO.CPP                                                          */
  12. /*                                                                       */
  13. /*    Copyright (C) 1992 Michael Newton and Comsoft Software             */
  14. /*    All Rights Reserved.                                               */
  15. /*                                                                       */
  16. /*-----------------------------------------------------------------------*/
  17.  
  18. #define Uses_TApplication
  19. #define Uses_TBackground
  20. #define Uses_TButton
  21. #define Uses_TCheckBoxes
  22. #define Uses_TCollection
  23. #define Uses_TColorGroup
  24. #define Uses_TColorItem
  25. #define Uses_TDeskTop
  26. #define Uses_TDialog
  27. #define Uses_TEvent
  28. #define Uses_TKeys
  29. #define Uses_TLabel
  30. #define Uses_TMenu
  31. #define Uses_TMenuBar
  32. #define Uses_TMenuItem
  33. #define Uses_TObject
  34. #define Uses_TPalette
  35. #define Uses_TRect
  36. #define Uses_TScreen
  37. #define Uses_TStatusDef
  38. #define Uses_TStatusItem
  39. #define Uses_TStatusLine
  40. #define Uses_TStaticText
  41. #define Uses_TSItem
  42. #define Uses_TSubMenu
  43. #define Uses_TStreamable
  44. #define Uses_TStreamableClass
  45. #define Uses_TView
  46. #define Uses_TWindow
  47. #define Uses_ipstream
  48. #define Uses_opstream
  49. #define Uses_fpstream
  50. #define Uses_MsgBox
  51.  
  52. #include <tv.h>
  53.  
  54. __link(RMenuBar)
  55. __link(RPatternButtons)
  56. __link(RPatternCluster)
  57. __link(RRadioButtons)
  58. __link(RResourceCollection)
  59. __link(RStaticText)
  60. __link(RVColorDialog)
  61.  
  62.  
  63. // CRTL header files ******************************************************
  64.  
  65. #if !defined __STDLIB_H
  66. #include <stdlib.h>
  67. #endif
  68.  
  69. #if !defined __STDIO_H
  70. #include <stdio.h>
  71. #endif
  72.  
  73. #if !defined __STDARG_H
  74. #include <stdarg.h>
  75. #endif
  76.  
  77. #if !defined __CONIO_H
  78. #include <conio.h>
  79. #endif
  80.  
  81. #if !defined __IOSTREAM_H
  82. #include <iostream.h>
  83. #endif
  84.  
  85. #if !defined __STRSTREA_H
  86. #include <strstrea.h>
  87. #endif
  88.  
  89. #if !defined __FSTREAM_H
  90. #include <fstream.h>
  91. #endif
  92.  
  93. #if !defined __IOMANIP_H
  94. #include <iomanip.h>
  95. #endif
  96.  
  97. #if !defined __STRING_H
  98. #include <string.h>
  99. #endif
  100.  
  101. #if !defined __DOS_H
  102. #include <dos.h>
  103. #endif
  104.  
  105. #if !defined __DIR_H
  106. #include <dir.h>
  107. #endif
  108.  
  109. #if !defined __ALLOC_H
  110. #include <alloc.h>
  111. #endif
  112.  
  113.  
  114. // non-CRTL header files **************************************************
  115.  
  116. #if !defined __TDEMO_HPP
  117. #include "tdemo.hpp"
  118. #endif
  119.  
  120. #if !defined __COLORDLG_HPP
  121. #include "colordlg.hpp"
  122. #endif
  123.  
  124.  
  125. // External variables *****************************************************
  126.  
  127. extern TPoint shadowSize;
  128.  
  129.  
  130. // TMenuItem **************************************************************
  131.  
  132. TMenuItem& operator +(TMenuItem& item1, TMenuItem& item2)
  133. {
  134.    TMenuItem *temp = &item1;
  135.    while(temp->next != NULL)
  136.       temp = temp->next;
  137.    temp->next = &item2;
  138.    return item1;
  139. }
  140.  
  141.  
  142. // TDemo ******************************************************************
  143.  
  144. const char * near TDemo::configFile = "TDEMO.CFG";
  145.  
  146. TDemo::TDemo() : TProgInit(initStatusLine, initMenuBar, initDeskTop)
  147. {
  148.    // This is the default palette for the application. If no configuration
  149.    // file is found, this palette will be copied into the applications
  150.    // palette.
  151.    // This palette was created by selecting "savePalette" on the menubar.
  152.    // SavePalette creates a text file named PALETTE.TXT, which will
  153.    // consist of an array similar to "defpal" below. The attributes in
  154.    // PALETTE.TXT will be the currently selected application's colors.
  155.  
  156.    uchar defPal[] = {0x09,0x17,0x18,0x1F,0x0E,0x04,0x0F,0x10,0x17,0x0E,
  157.                      0x09,0x1A,0x17,0x1C,0x1E,0x37,0x3F,0x0E,0x0C,0x13,
  158.                      0x30,0x3C,0x00,0x78,0x70,0x0E,0x0E,0x13,0x70,0x40,
  159.                      0x1E,0x78,0x70,0x0E,0x6E,0x4E,0x70,0x70,0x7E,0x7E,
  160.                      0x30,0x3E,0x3F,0x37,0x3B,0x78,0x70,0x7E,0x7E,0x03,
  161.                      0x4F,0x7E,0x5E,0x55,0x0B,0x3E,0x03,0x4F,0x0E,0x07,
  162.                      0x0F,0x00,0x00,0x37,0x3F,0x3A,0x13,0x13,0x30,0x3E,
  163.                      0x1E};
  164.  
  165.    // Call the restoreDesk function to restore the desktop
  166.    // If a valid configuration file exists, restoreDesk will restore
  167.    // the desktop. If not, then we set the defaults
  168.    if(!restoreDesk())
  169.       {
  170.       // Set background pattern character to ascii 206 ╬
  171.       deskTop->background->setPattern(206);
  172.       // Save the pattern character in member variable
  173.       currentPattern = deskTop->background->getPattern();
  174.       // TPatternButtons start at 4
  175.       patternButtons = 4;
  176.       // Copy the default palette into the application's palette
  177.       TPalette &palette = getPalette();
  178.       ushort length = palette[0];
  179.       memmove(&palette[1], &defPal[0], (size_t) length);
  180.       // Redraw the desktop
  181.       setState(sfExposed, False);
  182.       setState(sfExposed, True);
  183.       redraw();
  184.       }
  185. }
  186.  
  187.  
  188. TDemo::~TDemo()
  189. // For debugging only
  190. {
  191. }
  192.  
  193.  
  194. TMenuBar *TDemo::initMenuBar(TRect r)
  195. {
  196.    TMenuItem *sys =
  197.    new TMenuItem("~\360~", cmAbout, kbAltSpace, hcNone);
  198.    TMenuItem *exit =
  199.    new TMenuItem("E~x~it", cmQuit, kbAltX, hcNone);
  200.    TMenuItem *colors =
  201.    new TMenuItem("~C~olors", cmColors, kbNone, hcNone);
  202.    TMenuItem *pal =
  203.    new TMenuItem("~W~rite palette", cmSavPal, kbAltP, hcNone);
  204.  
  205.    TMenu *menu = new TMenu(*sys + *exit + *colors + *pal);
  206.    r.b.y = r.a.y + 1;
  207.  
  208.    return(new TMenuBar(r, menu));
  209. }
  210.  
  211.  
  212. TStatusLine *TDemo::initStatusLine(TRect r)
  213. {
  214.    r.a.y = r.b.y - 1;
  215.  
  216.    return new TStatusLine(r,
  217.       *new TStatusDef(0, 0xffff) +
  218.       *new TStatusItem("~Alt-X~ Exit", kbAltX, cmQuit) +
  219.       *new TStatusItem("~F10~ Menu", kbF10, cmMenu));
  220. }
  221.  
  222.  
  223. void TDemo::handleEvent(TEvent& event)
  224. {
  225.    // If user selects "Exit", save the current desktop
  226.    if(event.what == evCommand && event.message.command == cmQuit)
  227.       {
  228.       if(!confirmExit())          // Get confirmation
  229.          clearEvent(event);
  230.       else
  231.          saveDesk();              // Save desktop before exiting
  232.       }
  233.  
  234.    TApplication::handleEvent(event);
  235.  
  236.    switch(event.what)
  237.       {
  238.       case evCommand:
  239.          switch(event.message.command)
  240.             {
  241.             case cmSavPal:
  242.                writePalette("palette.txt");
  243.                break;
  244.             case cmAbout:
  245.                about();
  246.                break;
  247.             case cmColors:
  248.                colors();
  249.                break;
  250.             }
  251.          break;
  252.       default:
  253.          return;
  254.       }
  255.  
  256.    clearEvent(event);
  257. }
  258.  
  259.  
  260. void TDemo::about()
  261. {
  262.    char *line[] = {
  263.                   "TVColorDialog Demo",
  264.                   "(C) Copyright 1992",
  265.                   "Michael Newton and Comsoft Software",
  266.                   "All rights reserved.",
  267.                   "Demonstrates the TVColorDialog class."
  268.                   };
  269.  
  270.    TDialog *d = new TDialog(TRect(0, 0, 56, 13), "About");
  271.  
  272.    TStaticText *t;
  273.    int len = strlen(line[0]);
  274.    t = new TStaticText(TRect(0, 2, len, 3), line[0]);
  275.    t->options |= ofCenterX;
  276.    d->insert(t);
  277.  
  278.    len = strlen(line[1]);
  279.    t = new TStaticText(TRect(0, 4, len, 5), line[1]);
  280.    t->options |= ofCenterX;
  281.    d->insert(t);
  282.  
  283.    len = strlen(line[2]);
  284.    t = new TStaticText(TRect(0, 5, len, 6), line[2]);
  285.    t->options |= ofCenterX;
  286.    d->insert(t);
  287.  
  288.    len = strlen(line[3]);
  289.    t = new TStaticText(TRect(0, 6, len, 7), line[3]);
  290.    t->options |= ofCenterX;
  291.    d->insert(t);
  292.  
  293.    len = strlen(line[4]);
  294.    t = new TStaticText(TRect(0, 8, len, 9), line[4]);
  295.    t->options |= ofCenterX;
  296.    d->insert(t);